﻿// Licensed under GNU GPL V2.
// http://sps2010listmanager.codeplex.com/
// (c) 2010 by Daniel Bedarf

using System.Windows.Controls;
using System.Windows;
using System.Reflection;
using System.IO;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System;

namespace CodePlex.DBedarf.Sharepoint.ListManager
{
    /// <summary>
    /// an specialiced TreeViewItem wich contains Sharepoint - Objects and imnages
    /// </summary>
    class SPSTreeNode : TreeViewItem
    {
        /// <summary>
        /// SPSTreeNode Type
        /// </summary>
        public enum eTreeNodeType { None, Info, SPWeb, SPList, SPDocLib, SPField }

        /// <summary>
        /// Initializes a new instance of the <see cref="SPSTreeNode"/> class.
        /// </summary>
        public SPSTreeNode()
        {
            initHeader();
            NodeType = eTreeNodeType.None;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SPSTreeNode"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        public SPSTreeNode(eTreeNodeType type)
        {
            initHeader();
            NodeType = type;
        }

        TextBlock _text;
        Image _img;

        /// <summary>
        /// inits the header to show a picture
        /// </summary>
        private void initHeader()
        {
            StackPanel stack = new StackPanel();
            stack.Orientation = Orientation.Horizontal;
            base.Header = stack;
            _img = new Image();
            _img.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            _img.Margin = new Thickness(0, 0, 2, 0);

            stack.Children.Add(_img);
            _text = new TextBlock();
            _text.VerticalAlignment = VerticalAlignment.Center;
            stack.Children.Add(_text);
        }

        /// <summary>
        /// Gets or sets the text.
        /// </summary>
        /// <value>The text.</value>
        public string Text
        {
            set { _text.Text = value; }
            get { return _text.Text; }
        }


        /// <summary>
        /// blocks the header from overwriting
        /// </summary>
        /// <value></value>
        /// <returns>
        /// Ein Objekt, das das <see cref="T:System.Windows.Controls.HeaderedItemsControl"/> bezeichnet. Der Standardwert ist null. Ein Header kann eine Zeichenfolge oder ein <see cref="T:System.Windows.UIElement"/> sein.
        /// </returns>
        public new object Header
        {
            get
            {
                return base.Header;
            }
        }

        eTreeNodeType _nodetype;
        /// <summary>
        /// Gets or sets the type of the node.
        /// </summary>
        /// <value>The type of the node.</value>
        public eTreeNodeType NodeType
        {
            get
            {
                return _nodetype;
            }
            set
            {
                string path = "";
                switch (value)
                {
                    case eTreeNodeType.SPWeb:
                        path = "pack://application:,,,/Images/treeWeb.png";
                        break;
                    case eTreeNodeType.SPList:
                        path = "pack://application:,,,/Images/treeList.png";
                        break;
                    case eTreeNodeType.SPDocLib:
                        path = "pack://application:,,,/Images/treeDoc.png";
                        break;
                    default:
                        path= "";
                        break;
                }
                if (!string.IsNullOrEmpty(path))
                {
                    _img.Source = (ImageSource)new ImageSourceConverter().ConvertFromString(path);
                }
                _nodetype = value;
            }
        }


        /// <summary>
        /// Sets image to list-readonly.
        /// </summary>
        public void setListImageRO()
        {
            _img.Source = (ImageSource)new ImageSourceConverter().ConvertFromString("pack://application:,,,/Images/treeList_ro.png");
        }

        /// <summary>
        /// Sets image to list-readwrite
        /// </summary>
        public void setListImageRW()
        {
            _img.Source = (ImageSource)new ImageSourceConverter().ConvertFromString( "pack://application:,,,/Images/treeList.png");
        }

        /// <summary>
        /// Sets image to list-hidden
        /// </summary>
        public void setListImageHidden()
        {
            _img.Source = (ImageSource)new ImageSourceConverter().ConvertFromString("pack://application:,,,/Images/treeList_hidden.png");
        }

        /// <summary>
        /// Gets or sets the SPweb.
        /// </summary>
        /// <value>The SPweb.</value>
        public Microsoft.SharePoint.SPWeb SPWeb { get; set; }

        /// <summary>
        /// Gets or sets the SP list.
        /// </summary>
        /// <value>The SP list.</value>
        public Microsoft.SharePoint.SPList SPList { get; set; }

        /// <summary>
        /// Gets or sets the SP field.
        /// </summary>
        /// <value>The SP field.</value>
        public Microsoft.SharePoint.SPField SPField { get; set; }

    }
}
